package com.bj58.mis.apm.server.kylin.service.impl;

import com.bj58.mis.apm.server.kylin.mapper.KylinAnalysisMapper;
import com.bj58.mis.apm.server.kylin.model.Project;
import com.bj58.mis.apm.server.kylin.service.IKylinDataService;
import com.bj58.mis.apm.server.kylin.util.JsonUtil;
import com.google.gson.reflect.TypeToken;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Transactional
@Service
public class KylinDataServiceImpl implements IKylinDataService {

    @Resource
    private KylinAnalysisMapper kylinAnalysisMapper;

    @Override
    public void save(String dataType, String jsonData) {

        long now = new Date().getTime();
        Map<String, Object> updateparam = new HashMap<>();
        updateparam.put("record_update",now);

        List<Project> projects = saveProject(jsonData);
        kylinAnalysisMapper.saveOrUpdateProject(createUpdateMap(projects, now));
    }

    private List<Project> saveProject(String jsonData){
        return JsonUtil.fromJson(jsonData, new TypeToken<List<Project>>(){}.getType());
    }

    private Map<String, Object> createUpdateMap(Object obj, long now){
        Map<String, Object> map = new HashMap<>();
        map.put("list", obj);
        map.put("record_update", now);
        return map;
    }

}
